//
//  RWSettingViewController.m
//  RedWine
//
//  Created by Liufangfang on 2016/12/27.
//  Copyright © 2016年 HuiBangKe. All rights reserved.
//

#import "RWSettingViewController.h"
#import "AppDelegate.h"
#import "ClearCacheTool.h"
#import "DisclaimerViewController.h"
#import "AboutViewController.h"

@interface RWSettingViewController ()

@property (nonatomic, strong) NSArray *titiles;

@end

@implementation RWSettingViewController

#pragma mark - View LifeCycle

- (void)viewDidLoad {
    [super viewDidLoad];
    _titiles = @[@"清除本地缓存", @"消息通知提醒", @"当前版本",@"免责声明", @"关于我们"];
    [self setNavigation];
    [self setSettingUI];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Load Datas

#pragma mark - Config UI

- (void)setNavigation{
    
    self.title = @"设置";
}

- (void)setSettingUI{
    
    _settingTV = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, FTY_SCREEN_WIDTH, FTY_SCREEN_HEIGHT) style:UITableViewStylePlain];
    [self.view addSubview:_settingTV];
    _settingTV.separatorStyle = UITableViewCellSeparatorStyleNone;
    _settingTV.dataSource = self;
    _settingTV.delegate = self;
    _settingTV.scrollEnabled = NO;
    _settingTV.tableFooterView = [self createFooterUI];
}

#pragma mark - Config Invoke Methods

- (UIView *)createFooterUI{

    UIView *footerV = [[UIView alloc] initWithFrame:self.view.bounds];
    
    FTYButton *loginOutBtn = [FTYButton createButtonWithTitle:@"退出登录" tColor:[UIColor whiteColor] fontSize:15.*FTY_WIDTH_BASE backGroundColor:[FTYColor getMainColorWithAlpha:1.] cornerRadius:15.*FTY_WIDTH_BASE];
    [footerV addSubview:loginOutBtn];
    [loginOutBtn addTarget:self action:@selector(loginOutBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [loginOutBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_offset(260.*FTY_HEIGHT_BASE);
        make.left.mas_offset(10.*FTY_WIDTH_BASE);
        make.width.mas_offset(FTY_SCREEN_WIDTH - 2 * 10.*FTY_WIDTH_BASE);
        make.height.mas_offset(40.*FTY_HEIGHT_BASE);
    }];
    
    return footerV;
}

#pragma mark - Notificaiton Method

#pragma mark - Notificaiton Invoke Methods

#pragma mark - <UITableViewDataSource>

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return [_titiles count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 50.*FTY_HEIGHT_BASE;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    static NSString * const settingID = @"settingID";
    UITableViewCell *settingCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:settingID];
    settingCell.selectionStyle = UITableViewCellSelectionStyleNone;
    settingCell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    NSInteger row = indexPath.row;
    
    
    settingCell.textLabel.text = [_titiles objectAtIndex:row];
    settingCell.textLabel.font = FTY_FONTTSIZE(15);
    settingCell.detailTextLabel.font = FTY_FONTTSIZE(13);
    
    if (0 == row) {
        
        settingCell.accessoryType = UITableViewCellAccessoryNone;
        //settingCell.detailTextLabel.text = @"6.66MB";
        NSString *tmpPath = NSTemporaryDirectory();
        NSString *cachesPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
        NSString *docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
        NSString *cacheSize = [ClearCacheTool getCacheSizeWithFilePath:docPath];
        settingCell.detailTextLabel.text = cacheSize;
        
    } else if (1 == row) {
        
        settingCell.accessoryType = UITableViewCellAccessoryNone;
        UISwitch *pushST = [[UISwitch alloc] initWithFrame:CGRectMake(FTY_SCREEN_WIDTH - 70.*FTY_WIDTH_BASE, (CGRectGetHeight(settingCell.frame) - 30.*FTY_HEIGHT_BASE) / 2, 100.*FTY_WIDTH_BASE, 20.*FTY_HEIGHT_BASE)];
        [settingCell addSubview:pushST];
        //默认时颜色
        pushST.backgroundColor = [FTYColor getColor:@"e2e2e2" andAlpha:1.];
        pushST.layer.masksToBounds = YES;
        pushST.layer.cornerRadius = pushST.bounds.size.height/2;
        //边缘颜色
        pushST.tintColor = [FTYColor getColor:@"e2e2e2" andAlpha:1.];
        //开启时颜色
        pushST.onTintColor = [FTYColor getColor:@"32cd00" andAlpha:1.];
    } else if (2 == row){
        NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
        // app版本
        NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
        // app build版本
        NSString *app_build = [infoDictionary objectForKey:@"CFBundleVersion"];
        settingCell.detailTextLabel.text = app_build;
        
    }
    
    //底部线
    UIView *line = [[UIView alloc] init];
    [settingCell addSubview:line];
    line.backgroundColor = [FTYColor lightGrayColor];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.mas_offset(0);
        make.height.mas_offset(.5*FTY_HEIGHT_BASE);
    }];
    
    return settingCell;
}

#pragma mark - <UITableViewDelegate>
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (0 == indexPath.row) {
        NSString *tmpPath = NSTemporaryDirectory();

        NSString *cachesPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
        NSString *docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
        [[FTYToastView alloc] addTitle:@"清除缓存" addView:self.view];
        [_settingTV reloadData];
        [ClearCacheTool clearCacheWithFilePath:docPath];
    }else if (3 == indexPath.row){
    
        DisclaimerViewController *viewController = [[DisclaimerViewController alloc] init];
        [self.navigationController pushViewController:viewController animated:YES];
    }else if (4 == indexPath.row){
        AboutViewController *viewController = [[AboutViewController alloc] init];
        [self.navigationController pushViewController:viewController animated:YES];
    }
    
    
}

#pragma mark - Private Invoke Methods

- (void)loginOutBtnAction:(UIButton *)btn{

    if ([FTYUser sharedUser].userID.length > 0) {
        
        //[[FTYToastView alloc] addTitle:@"成功退出登录" addView:self.view];
        
//        //清空本地个人信息存储
//        [[FTYUser sharedUser] userLogOut];
        
        //请求退出登录接口
        FTYUser *user = [FTYUser sharedUser];
        [FTYHTTPManager postInfoWithSubUrl:[NSString stringWithFormat:@"/shop/ybkapi.php?act=ybkapi&op=logout&member_id=%@&token=%@", user.userID, user.userToken] parameters:nil block:^(id result, NSError *error) {
            
            if (!error) {
                
                NSInteger codeInfo = [[[result objectForKey:@"err"] objectForKey:@"errorcode"] integerValue];
                NSString *info = [[result objectForKey:@"err"] objectForKey:@"errorinfo"];
                NSDictionary *infoDic = nil;
                if (0 == codeInfo) {
                    //[[FTYToastView alloc] addTitle:@"退出成功" addView:self.view];
                    [self.navigationController popToRootViewControllerAnimated:YES];
                } else {
                    [[FTYToastView alloc] addTitle:info addView:self.view];
                }
            } else {
                NSLog(@"======%@", error);
            }
        }];
    } else {
     
        [[FTYToastView alloc] addTitle:@"已退出" addView:self.view];
    }
    //清空本地个人信息存储
    [[FTYUser sharedUser] userLogOut];
    
    [(AppDelegate *)[UIApplication sharedApplication].delegate showWindowHome:@"loginOut"];
   

}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
